/**
 * Created by Administrator on 2015/6/11.
 */
import com.sun.xml.internal.ws.api.ha.StickyFeature;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.*;
public class SqliteTest {
    public static void main(String[] args) throws Exception {
        saveJpg("thumb.jpg","test.db");
    }
    public static void saveJpg(String fname,String dbfile) throws ClassNotFoundException, SQLException, FileNotFoundException {
        //存放一条基础数据做测试
        Class.forName("org.sqlite.JDBC");
        String driverName="jdbc:sqlite:"+dbfile;
        Connection conn=DriverManager.getConnection(driverName);
        PreparedStatement ps=conn.prepareStatement("insert into picTable values (?,?);");
        File file =  new File(fname);
        InputStream in = new FileInputStream(file);

        ps.setInt(1,3);
        ps.setBinaryStream(2,in,(int)file.length());
        ps.execute();
        conn.close();


        }

    public static void test() throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection conn =
                DriverManager.getConnection("jdbc:sqlite:test.db");
        Statement stat = conn.createStatement();
        stat.executeUpdate("drop table if exists people;");
        stat.executeUpdate("create table people (name, occupation);");
        PreparedStatement prep = conn.prepareStatement(
                "insert into people values (?, ?);");

        prep.setString(1, "Gandhi");
        prep.setString(2, "politics");
        prep.addBatch();
        prep.setString(1, "Turing");
        prep.setString(2, "computers");
        prep.addBatch();
        prep.setString(1, "Wittgenstein");
        prep.setString(2, "smartypants");
        prep.addBatch();

        conn.setAutoCommit(false);
        prep.executeBatch();
        conn.setAutoCommit(true);

        ResultSet rs = stat.executeQuery("select * from people;");
        while (rs.next()) {
            System.out.println("name = " + rs.getString("name"));
            System.out.println("job = " + rs.getString("occupation"));
        }
        rs.close();
        conn.close();
    }
}